﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace WindowsFormsApplication1

{
    public partial class Invoice : Form
    {
        public Invoice()
        {
            InitializeComponent();
        }

        private void Invoice_Load(object sender, EventArgs e)
        {

        }

        private void Btn_Isubmit_Click(object sender, EventArgs e)
        {

            if ( Txt_Ino.Text.Trim().Length <= 0)
            {
                MessageBox.Show(new Form() { TopMost = true }, "Please Enter Invoice No ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Txt_Ino.Select();
                Txt_Ino.Focus();
            }
            else
            {
                if (Txt_Idn .Text.Trim().Length <= 0)
                {
                    MessageBox.Show(new Form() { TopMost = true }, "Please Enter Dealer Name ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Txt_Idn.Select();
                    Txt_Idn.Focus();
                }
                else
                {
                    if (Txt_Iprice .Text.Trim().Length <= 0)
                    {
                        MessageBox.Show(new Form() { TopMost = true }, "Please Enter Price ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Txt_Iprice.Select();
                        Txt_Iprice.Focus();
                    }
                    //else
                    //{
                    //    if (txtpass.Text.Trim().Length <= 0)
                    //    {
                    //        MessageBox.Show(new Form() { TopMost = true }, "Please Enter Password ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //        txtpass.Select();
                    //        txtpass.Focus();
                    //    }
                    else
                    {
                        try
                        {

//////////////////////////////number	varchar(50)	Unchecked
//////////////////////////////date	datetime	Unchecked
//////////////////////////////price	real	Unchecked
//////////////////////////////createdby	varchar(50)	Unchecked
//////////////////////////////createddate	datetime	Unchecked
//////////////////////////////updatedby	varchar(50)	Checked
//////////////////////////////updateddate	datetime	Checked

                            String d = Txt_Idn.Text.Trim();
                            d = d.Substring(0,d.IndexOf(" - "));


                            String val = Txt_Ino.Text.Trim();
                            try
                            {
                                val = val.Substring(0, val.IndexOf(" - "));
                            }
                            catch
                            {
                                val = Txt_Ino.Text.Trim();
                            }

                            String updUser = "UPDATE invoice SET number = '" + val + "',price = '" + Txt_Iprice.Text.Trim() + "',status='" + cmbStatus.SelectedItem.ToString() + "',dealer='"+d+"',date = '" + Dte_Idte.Value + "',updatedby = '" + new Midval().getUser() + "',updateddate = '" + DateTime.Now + "' WHERE number = '" + val + "'";

                            String insUser = "INSERT INTO invoice (number,date,price,dealer,status,createdby,createddate) VALUES ('" + val + "','" + Dte_Idte.Value + "','" + Txt_Iprice.Text.Trim() + "' ,'" + d + "','" + cmbStatus.SelectedItem.ToString() + "','" + new Midval().getUser() + "','" + DateTime.Now + "') ";


                            Boolean upd = DbConnet.AddEditDel(updUser);
                            Boolean ins = false;

                            if (!upd)
                                ins = DbConnet.AddEditDel(insUser);

                            if (upd || ins)
                            {
                                MessageBox.Show(new Form() { TopMost = true }, "Invoice Created/Updated Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                Txt_Idn.Clear();
                                Txt_Ino.Clear();
                                Txt_Iprice.Clear();
                                //DateTime d = DateTime.Now();

                            }

                        }
                        catch (Exception exins)
                        {
                        }

                    }
                }
            }

        }

        private void Btn_Icancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void Txt_Idn_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            dealer_popup dp = new dealer_popup();
            dp.ShowDialog();
            Txt_Idn.Text = new Midval().getDeCode();
        }

        private void Txt_Ino_TextChanged(object sender, EventArgs e)
        {
            try
            {
                String val = Txt_Ino.Text.Trim();
                val = val.Substring(0, val.IndexOf(" - "));

                String getDetail = "SELECT number,date,price ,dealer,status " +
                                   " FROM invoice WHERE number = '" + val + "'";
                String dealer = "";

                SqlDataReader ddr = DbConnet.ReadTable(getDetail);

                while (ddr.Read())
                {
                    String d = ddr["date"].ToString();
                    Dte_Idte.Value = DateTime.Parse(d);
                    Txt_Iprice.Text = ddr["price"].ToString();
                    dealer = ddr["dealer"].ToString();
                    switch (ddr["status"].ToString())
                    {
                        case"Open":
                            cmbStatus.SelectedIndex = 0;
                            break;
                        case"Close":
                            cmbStatus.SelectedIndex = 1;
                            break;
                    }
                }
                ddr.Close();

                String name = "SELECT name FROM dealer WHERE code = '" + dealer + "'";
                SqlDataReader nr = DbConnet.ReadTable(name);
                if (nr.Read())
                {
                    Txt_Idn.Text = dealer + " - " + nr["name"].ToString();
                }
                nr.Close();
            }
            catch
            {
            }
        }

        private void Txt_Ino_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                inv_pop ip = new inv_pop();
                ip.ShowDialog();
                String val = new Midval().getIvalue();
                Txt_Ino.Text = val;
            }
            catch
            {
            }
        }//close tab
    }
}
